package gui;

import gui.TableModel;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.Dimension;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import types.Menu;
import types.MenuBar;
import types.MenuItem;

import java.awt.Font;
import java.awt.Color;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

public class FrmMenues extends JDialog implements ISubject, IObserver
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton btnAdd = null;
	private JButton btnDelete = null;
	private JButton btnOK = null;
	private JButton btnCancel = null;
	private JPanel jPanel = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private Vector<IObserver> observers = null;
	Vector<Menu> menues = null;  //  @jve:decl-index=0:
	private JLabel jLabel = null;

	/**
	 * @param owner
	 */
	public FrmMenues(/*Frame owner*/)
	{
		super(/*owner*/);
		observers = new Vector<IObserver>(1,1);
		menues = new Vector<Menu>();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(371, 316);
		this.setContentPane(getJContentPane());
		this.setTitle("Menues");
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		setLocationRelativeTo(null);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes btnAdd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnAdd()
	{
		if (btnAdd == null)
		{
			btnAdd = new JButton();
			btnAdd.setText("Add");
			btnAdd.setBounds(new Rectangle(74, 237, 56, 26));
			btnAdd.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					openMenuForm();
					
//					final TableModel tableModel = (TableModel) jTable.getModel();
//					int count = tableModel.getRowCount();
//					tableModel.setRowCount(count + 1);
//					tableModel.setValueAt(txtText.getText(),count,0);
//					tableModel.setValueAt(Integer.parseInt(cbxLevel.getSelectedItem().toString()),count,1);
//					
//					jTable.repaint();
					
				}
			});
		}
		return btnAdd;
	}

	protected void openMenuForm()
	{
		FrmMenu frm = new FrmMenu();
		frm.registerInterest(this);
		frm.setVisible(true);
	}

	/**
	 * This method initializes btnDelete	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnDelete()
	{
		if (btnDelete == null)
		{
			btnDelete = new JButton();
			btnDelete.setText("Delete");
			btnDelete.setBounds(new Rectangle(135, 237, 70, 26));
			btnDelete.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					final TableModel tableModel = (TableModel) jTable.getModel();
					int selected = tableModel.getRow();
					
					if(selected<0)
						return;
					
					for (int i = 0; i < menues.size(); i++)
					{
						if(menues.get(i).getText().equals(tableModel.getValueAt(selected, 0)))
						{
							menues.remove(i);
							break;
						}
					}
					
					tableModel.removeRow(selected);
					
					
					jTable.repaint();
				}
			});
		}
		return btnDelete;
	}

	/**
	 * This method initializes btnOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOK()
	{
		if (btnOK == null)
		{
			btnOK = new JButton();
			btnOK.setText("OK");
			btnOK.setBounds(new Rectangle(210, 237, 51, 26));
			btnOK.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
//					Vector<Menu> items = getMenues();
					
					MenuBar bar = new MenuBar(menues);

					for(int i=0;i<observers.size();i++)
						observers.get(i).takeResult(bar, "MenuBar");
					
					dispose();
					return;
				}

			});
		}
		return btnOK;
	}
	
//	protected Vector<Menu> getMenues()
//	{
//		final TableModel tableModel = (TableModel) jTable.getModel();
//		menues = new Vector<Menu>();
////		for (int i = 0; i < tableModel.getRowCount(); i++)
////		{
////			Menu item = new Menu(
////					Integer.parseInt(tableModel.getValueAt(i, 1).toString()),
////					tableModel.getValueAt(i, 0).toString()
////					);
////			
////			vec.addElement(item);
////		}
//		
//		return menues;
//	}


	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel()
	{
		if (btnCancel == null)
		{
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.setBounds(new Rectangle(266, 237, 73, 26));
			btnCancel.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					dispose();
					return;
				}
			});
			
		}
		return btnCancel;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel()
	{
		if (jPanel == null)
		{
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 9, 45, 16));
			jLabel.setText("Menues");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(4, 8, 350, 272));
			jPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			jPanel.add(getBtnAdd(), null);
			jPanel.add(getBtnDelete(), null);
			jPanel.add(getJScrollPane(), null);
			jPanel.add(getBtnOK(), null);
			jPanel.add(getBtnCancel(), null);
			jPanel.add(jLabel, null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane()
	{
		if (jScrollPane == null)
		{
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(8, 30, 333, 197));
			jScrollPane.setViewportView(getTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes table	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTable()
	{
		if (jTable == null)
		{
			final TableModel tableModel = new TableModel();
			tableModel.addColumn("Text");
			tableModel.addColumn("Level");
			tableModel.setRowCount(0);
			tableModel.initTableModel();

			jTable = new JTable(tableModel);
			jTable.setCellSelectionEnabled(false);
			jTable.setRowSelectionAllowed(true);
			jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jTable.setColumnSelectionAllowed(false);
			jTable.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mousePressed(java.awt.event.MouseEvent e)
				{
					tableModel.setRow(jTable.rowAtPoint(new Point(e.getX(),e.getY())));
				}
			});
		}
		return jTable;
	}

	@Override
	public void registerInterest(IObserver obs)
	{
		observers.addElement(obs);
		
	}

	@Override
	public void takeResult(Object result, String type)
	{
		menues.addElement((Menu)result);	
		final TableModel tableModel = (TableModel) jTable.getModel();
		
		int count = tableModel.getRowCount();
		tableModel.setRowCount(count+1);
		tableModel.setValueAt(((Menu)result).getText(), count, 0);
		tableModel.setValueAt(((Menu)result).getLevel(), count, 1);
		
		jTable.repaint();
	}

	@Override
	public void executeCommand(Object obj)
	{
		// TODO Auto-generated method stub
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
